package chd.edu.spzx.manager.service.impl;

import chd.edu.spzx.manager.mapper.SysRoleMenuMapper;
import chd.edu.spzx.manager.service.SysMenuService;
import chd.edu.spzx.manager.service.SysRoleMenuService;
import chd.edu.spzx.model.dto.system.AssignMenuDto;
import chd.edu.spzx.model.entity.system.SysMenu;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author Zhang Chaoliang
 * create 2023
 */
@Service
public class SysRoleMenuServiceImpl implements SysRoleMenuService {

    @Autowired
    private SysRoleMenuMapper sysRoleMenuMapper;

    @Autowired
    private SysMenuService sysMenuService;


    /**
     * 查询所有的菜单并且查询角色分配过的菜单
     * @param roleId
     * @return
     */
    @Override
    public Map<String, Object> findSysRoleMenuByRoleId(Long roleId) {
        //查询所有的菜单
        List<SysMenu> sysMenuList = sysMenuService.findNodes();

        //查询角色分配过的菜单
        List<Long> roleMenuIds = sysRoleMenuMapper.findSysRoleMenuByRoleId(roleId);

        //将数据存储到Map中进行返回
        Map<String , Object> map = new HashMap<>() ;
        map.put("sysMenuList" , sysMenuList) ;
        map.put("roleMenuIds" , roleMenuIds) ;
        return map;
    }



    /**
     * 保存角色分配菜单数据
     * @return
     */
    @Override
    public void doAssign(AssignMenuDto assignMenuDto) {
        //删除角色之前分配的菜单数据
        sysRoleMenuMapper.deleteByRoleId(assignMenuDto.getRoleId());

        //保存分配的数据
        List<Map<String, Number>> menuInfo = assignMenuDto.getMenuIdList();
        if(menuInfo != null && menuInfo.size() > 0) {
            sysRoleMenuMapper.doAssign(assignMenuDto) ;
        }
    }

}
